package com.test.www.service;

import com.test.www.entity.Admin;
import com.test.www.util.FileUtil;
import com.test.www.util.IArrayList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * <p>学生管理系统登录</p>
 * @author
 * 2020/12/16 22:26
 */
public class Login extends JFrame {
    JPanel jp, jp1, jp2, jp3, jp4;
    public Login(){
        JFrame jFrame = new JFrame("学生成绩管理系统（登录页面）");

        jp  = new JPanel();
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();
        JPopupMenu jPopupMenu = new JPopupMenu();

        JLabel jLabel= new JLabel("请输入名称和密码进行登录");
        JTextField jTextField = new JTextField();
        jTextField.setPreferredSize(new Dimension (300,30));
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setPreferredSize(new Dimension (300,30));
        JLabel jLabel1 = new JLabel("名称：");
        JLabel jLabel2 = new JLabel("密码：");
        JButton jButton = new JButton("登录");
        JLabel jLabel3 = new JLabel("若无账号，可使用学生成绩管理系统体验账号：名称=admin，密码=123456");


        jFrame.setSize(850, 550);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        jTextField.setColumns(20);
        jPasswordField.setColumns(20);
        jPasswordField.setSize(80,20);
        //文字大小设置
        Font font=new Font("宋体",Font.BOLD,24);
        jLabel.setFont(font);
        jLabel1.setFont(font);
        jLabel2.setFont(font);
        jButton.setFont(font);
        //将页面分成五行一列
        jFrame.setLayout(new GridLayout(5,1));
        // 登录
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (loginCheck(jTextField.getText(),jPasswordField.getText())){
                        new Home();
                        jFrame.dispose();
                    }else{
                        jPopupMenu.removeAll();
                        jPopupMenu.add("用户名或密码错误");
                        jPopupMenu.show(jFrame,360,250);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });

        jp.add(jLabel);
        jp1.add(jLabel1);
        jp1.add(jTextField);
        jp2.add(jLabel2);
        jp2.add(jPasswordField);
        jp3.add(jButton);
        jp4.add(jLabel3);
        
        jFrame.add(jp);
        jFrame.add(jp1);
        jFrame.add(jp2);
        jFrame.add(jp3);
        jFrame.add(jp4);
        jFrame.setVisible(true);
    }
    
    /*
     * 登录验证
     */
    public boolean loginCheck(String username,String password) throws IOException {
        IArrayList<Admin> list = FileUtil.getInstance().getAdmin();
        for (Admin admin : list) {
            if (admin.getUsername().equals(username) && admin.getPassword().equals(password)){
                return true;
            }
        }
        return false;
    }
}
